<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #operationBeforeExtend="{ record }">
        <!-- <a-link @click="openPaiSong(record)" >打印</a-link> -->
        <a-link @click="handleCarList(record)">指派司机</a-link>
      </template>
    </ma-crud>
    <!-- 指派司机 -->
    <a-modal :mask-closable="false"  v-model:visible="showZhipai" title="指派司机" @cancel="showZhipai = false" width="800px" :on-before-ok="handleOk">
      <div class="flex align_center" v-if="showZhipai">
        <div style="width: 100px;" >选择司机：</div>
        <a-select :options="carList" v-model="car_no" :filter-option="filterOption" :field-names="{value: 'car_no_id', label: 'car_nos'}" :style="{flex:1}"  placeholder="请选择"  />
      </div>
    </a-modal>

    <a-modal :mask-closable="false" 
			v-model:visible="paiSongShow"
			width="auto"
			title="打印接取单"
			ok-text="打印"
			:footer="false"
		>
			<paisong :row="paiSongList" :user="user" v-if="paiSongShow"></paisong>
		</a-modal>
  </div>
</template>
<script setup>
import { ref, reactive,onMounted } from 'vue'
import sendForm from '@/api/send/sendForm'
import sendCar from '@/api/send/sendCar'
import { pinyin } from "pinyin-pro"
import { request } from "@/utils/request.js";
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'
import { useUserStore } from "@/store";
const userStore = useUserStore();
// 当前登录的用户所有信息，包括 id 用户名 昵称 等信息
const user = userStore.user;
const crudRef = ref()
const showZhipai = ref(false)
const carList = ref([])
const car_no = ref('')
const itemData = ref({})
const filterOption = (v,option)=>{
  return option.keywords.some(keywords=>keywords.includes(v))
}
const paiSongList = ref({})
const paiSongShow = ref(false)
const handleCarList = (row)=>{
  request({
        url: 'prediction/driver/index',
        method: 'get',
        params:{
          page:1,
          pageSize:1000,
          dept_id:row.take_dept_id
        }
    }).then(res=>{
        console.log(res)
        let _data = res.data.items
        _data.map((v,i)=>{
          v.car_nos = v.car[0].car_no + '--' + v.driver_name
          v.car_no_id = v.car[0].id
        })
        _data.map((v,i)=>{
              v.keywords = [
                v.car_nos,
                pinyin(v.car_nos,{toneType:'none',pattern:'first'}).split(' ').join(''),
                pinyin(v.car_nos,{toneType:'none',pattern:'first'}).split(' ').join('').toUpperCase()
              ]
        })
        itemData.value = row
        carList.value = _data
        showZhipai.value = true
    })
}
const handleOk = async ()=>{
  console.log('132132')
  let res = await sendCar.save({send_id:itemData.value.send_id,send_form_id:itemData.value.id,car_id:car_no.value})
  if(res.code == 200){
    Message.success('指派成功')
    crudRef.value.refresh()
    return true
  }else{
    Message.error(res.message)
    return false
  }
  
}

const openPaiSong = (item) =>{ // 打印派送单
		item.detail.map((v,i)=>{
			if(v.prediction_id == 0){
				v.prediction = v.wareuserdetail
			}
		})
		paiSongList.value = item
		paiSongShow.value = true
	}
const numberOperation = (newValue, id, numberName) => {
  sendForm.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  sendForm.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'flyos_send_form',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: sendForm.getList,
  recycleApi: sendForm.getRecycleList,
  add: {
    show: false,
    api: sendForm.save,
    auth: ['send:form:save']
  },
  edit: {
    show: false,
    api: sendForm.update,
    auth: ['send:form:update']
  },
  delete: {
    show: false,
    api: sendForm.deletes,
    auth: ['send:form:delete'],
    realApi: sendForm.realDeletes,
    realAuth: ['send:form:realDeletes']
  },
  recovery: {
    show: false,
    api: sendForm.recoverys,
    auth: ['send:form:recovery']
  },
  import: {
    show: false,
    url: 'send/form/import',
    templateUrl: 'send/form/downloadTemplate',
    auth: ['send:form:import']
  },
  export: {
    show: false,
    url: 'send/form/export',
    auth: ['send:form:export']
  }
})

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  {
    title: "派车单单号",
    dataIndex: "order_no",
    formType: "input",
    search: true
  },
  {
    title: "发货单位",
    dataIndex: "dept_id",
    formType: "select",
    search: true,
    dict: {
      // 远程通用接口请求，新版代码生成都有一个 remote 接口
      remote: "system/dept/remote",
      // 指定组件接收的props
      props: { label: "name", value: "id" },
      // 开启分页
      openPage: false,
      // 远程请求配置项
      remoteOption: {
        // 按用户名排序
        sort: { id: "desc" }, // 如果不指定排序方式，默认为正序排序
        // 设置查询的字段
        select: ["id", "name"],
        // 设置数据过滤
        filter: {
          type: ["=", 2],
          status: ['=',1]
        },
        // 关联模型
        relations: [],
      },
      translation: true,
    },
  },
  {
    title: "发货单位电话",
    dataIndex: "dept_mobile",
    formType: "input",
    search: true
  },
  {
    title: "接取单位",
    dataIndex: "take_dept_id",
    formType: "select",
    search: true,
    dict: {
      // 远程通用接口请求，新版代码生成都有一个 remote 接口
      remote: "system/dept/remote",
      // 指定组件接收的props
      props: { label: "name", value: "id" },
      // 开启分页
      openPage: false,
      // 远程请求配置项
      remoteOption: {
        // 按用户名排序
        sort: { id: "desc" }, // 如果不指定排序方式，默认为正序排序
        // 设置查询的字段
        select: ["id", "name"],
        // 设置数据过滤
        filter: {
          type: ["=", 3],
          status: ['=',1]
        },
        // 关联模型
        relations: [],
      },
      translation: true,
    },
  },
  {
    title: "接取详细地址",
    dataIndex: "detail_address",
    formType: "input",
  },
  {
    title: "收货单位名称",
    dataIndex: "take_dept_name",
    formType: "input",
    search: true
  },
  {
    title: "收货单位电话",
    dataIndex: "take_mobile",
    formType: "input",
    search: true
  },
  {
    title: "派单时间",
    dataIndex: "send_time",
    formType: "date",
    search: true,
    showTime: true
  },
  {
    title: "派单状态",
    dataIndex: "form_status",
    formType: "radio",
    search: true,
    dict: {
      name: "form_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    }
  },
  // {
  //   title: "打印次数",
  //   dataIndex: "print_count",
  //   formType: "input",
  //   search: true
  // },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "修改者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
</script>
<script> export default { name: 'send:form' } </script>